module WWW
  class Tkd
    class TbBuyerOrder < ActiveRecord::Base
      @@LOG_FLAG="TbBuyerOrder"
      #根据最后修改时间对Seller Order进行排序并搜索
      def self.find_on_page(user_id,page_num, page_size)

        find(:all,

        :conditions => "user_id = "+user_id.to_s,

        :order => "updated_at desc",

        :limit => page_size,

        :offset => page_num)

      end
      #判断订单是否存在
      def self.find_from_order_id_and_user_id(order_id,user_id)
        return self.find_by_order_id_and_user_id(order_id,user_id)
      end
      #计算总数
      def self.count(user_id)
        return self.count_by_sql(["select count(id) from tb_buyer_orders where user_id = ?", user_id])
      end
      #買家自动关联卖家设置的快递单信息
      def self.auto_relate_seller_express_id buyer_order,order
            tb_user = WWW::Tkd::TbUser.find_by_domain_login_id order.seller_nick
            if tb_user!=nil
                    seller_order = WWW::Tkd::TbSellerOrder.find_by_order_id_and_user_id(order.id,tb_user.id)
                    if seller_order!=nil
                       ori_express_id = buyer_order.express_id
                       if ori_express_id!=seller_order.express_id#只有快遞更新後才會更改
                                buyer_order.is_newer = true
                                buyer_order.express_id = seller_order.express_id
                                buyer_order.save
                                #記錄買家自動關聯到賣家設置的快遞信息
                                WWW::Tkd::Logger.debug  "#{@@LOG_FLAG}buyer_auto_relate_seller_express","seller_id:#{seller_order.user_id},buyer_id:#{buyer_order.user_id},express_id:#{seller_order.express_id}"
                       end
                    end
            end
      end
      #将Taobao的数据导入到tb_order表中，并与tb_seller_order进行关联
      def self.save_from_tabao items,session
        if items['trade']!=nil
          conv = Iconv.new("UTF-8","GB2312")
          items['trade'].each do|item|
            order = WWW::Tkd::TbOrder.find_by_tid item['tid'][0].strip().to_s
            if order==nil
              order = WWW::Tkd::TbOrder.new
              order.seller_nick = item['seller_nick'][0].strip() unless item['seller_nick']==nil
              order.buyer_nick = item['buyer_nick'][0].strip() unless item['buyer_nick']==nil
              order.iid = item['iid'][0].strip() unless item['iid']==nil
              order.title=item['title'][0].strip() unless item['title']==nil
              order.price=item['price'][0].strip() unless item['price']==nil
              order.pic_path=item['pic_path'][0].strip() unless item['pic_path']==nil
              order.num=item['num'][0].strip() unless item['num']==nil
              order.tid=item['tid'][0].strip() unless item['tid']==nil
              order.order_created_at=item['created'][0].strip() unless item['created']==nil
              order.status=item['status'][0].strip() unless item['status']==nil
              order.seller_rate=item['seller_rate'][0].strip() unless item['seller_rate']==nil
              order.buyer_rate=item['buyer_rate'][0].strip() unless item['buyer_rate']==nil
              order.save
            end
            #将order与user关联到seller_order表中
            buyer_order = self.find_by_user_id_and_order_id(session[:id],order.id)
            if buyer_order==nil
              buyer_order=self.new
              buyer_order.user_id=session[:id]
              buyer_order.order_id=order.id
              buyer_order.save
            end
           auto_relate_seller_express_id buyer_order,order
          end
        end
      end
    end
  end
end
